package cn.edu.sgu.www.mhxysy.service.school.impl;

import cn.edu.sgu.www.mhxysy.consts.CustomDirectories;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.school.SchoolDTO;
import cn.edu.sgu.www.mhxysy.entity.school.School;
import cn.edu.sgu.www.mhxysy.mapper.school.SchoolMapper;
import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.pager.school.SchoolPager;
import cn.edu.sgu.www.mhxysy.service.school.SchoolService;
import cn.edu.sgu.www.mhxysy.util.ImageUpdater;
import cn.edu.sgu.www.mhxysy.util.ObjectUtils;
import cn.edu.sgu.www.mhxysy.util.UploadUtils;
import cn.edu.sgu.www.mhxysy.util.WrapperUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.function.Consumer;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Slf4j
@Service
public class SchoolServiceImpl implements SchoolService {

    private final UploadUtils uploadUtils;
    private final ImageUpdater imageUpdater;
    private final SchoolMapper schoolMapper;

    @Autowired
    public SchoolServiceImpl(
            UploadUtils uploadUtils,
            ImageUpdater imageUpdater,
            SchoolMapper schoolMapper) {
        this.uploadUtils = uploadUtils;
        this.imageUpdater = imageUpdater;
        this.schoolMapper = schoolMapper;
    }

    @Override
    public String upload(MultipartFile file) throws IOException {
        String directory = CustomDirectories.DIRECTORY_JSMP;

        return uploadUtils.upload(file, directory);
    }

    @Override
    public void insert(SchoolDTO schoolDTO) {
        School school = toEntity(schoolDTO);

        school.setId(null);

        schoolMapper.insert(school);
    }

    @Override
    public void updateById(SchoolDTO schoolDTO) {
        ObjectUtils.requireNonNull(schoolDTO.getId(), CustomMessages.ID_NOT_NULL);

        School school = toEntity(schoolDTO);

        schoolMapper.updateById(school);
    }

    @Override
    public void updateImageById(Integer id, String image) {
        imageUpdater.updateImageById(image, new Consumer<Object>() {
            @Override
            public void accept(Object o) {
                UpdateWrapper<School> updateWrapper = WrapperUtils.getUpdateWrapper(id, image);

                schoolMapper.update(updateWrapper.getEntity(), updateWrapper);
            }
        });
    }

    @Override
    public List<School> selectAll() {
        return schoolMapper.selectList(null);
    }

    @Override
    public School selectById(Integer id) {
        return schoolMapper.selectById(id);
    }

    @Override
    public Page<School> selectByPage(SchoolPager pager) {
        QueryWrapper<School> queryWrapper = new QueryWrapper<>();
        Page<School> page = Pager.ofPage(pager);

        queryWrapper.eq(
                pager.getPhyle() != null,
                "phyle", pager.getPhyle()
        );
        queryWrapper.eq(
                pager.getGender() != null,
                "gender", pager.getGender()
        );

        return schoolMapper.selectPage(page, queryWrapper);
    }

    private School toEntity(SchoolDTO schoolDTO) {
        School school = new School();

        school.setId(schoolDTO.getId());
        school.setName(schoolDTO.getName());
        school.setNote(schoolDTO.getNote());
        school.setPhyle(schoolDTO.getPhyle());
        school.setGender(schoolDTO.getGender());

        return school;
    }

}